/*----------------------------------------------------------------------------
Owner(s): Shane Whitfield
$Copyright: $
$Id: $
----------------------------------------------------------------------------*/

#include "soundmgr.h"
#include <string.h>

/*--------------------------------------------------------------------------*/

static bool (*s_systemSoundLoader)(const char* i_filename, unsigned int& o_soundId) = 0 ;
static bool (*s_systemSoundPlay)(unsigned int i_soundId) = 0 ;

/*--------------------------------------------------------------------------*/
CSound::CSound(unsigned int i_soundId)
{
	m_soundId = i_soundId;
}

void CSound::Play()
{
	s_systemSoundPlay(m_soundId);
}

void CSound::Stop()
{
	s_systemSoundPlay(m_soundId);
}

/*--------------------------------------------------------------------------*/

CSoundMgr::CSoundMgr() : super("SoundMgr")
{
	m_state = EState_Mute;
}

void CSoundMgr::SetSystemSoundLoader(bool (*systemsoundloader)(const char* i_filename, unsigned int& o_soundId))
{
    s_systemSoundLoader = systemsoundloader;
}

void CSoundMgr::SetSystemSoundPlay(bool (*systemsoundplay)(unsigned int i_soundId))
{
    s_systemSoundPlay = systemsoundplay;
}

void CSoundMgr::On_Command(const char* i_command, const char* i_parameters)
{
	if (!strcmp(i_command, "on"))
	{
		setState(EState_On);
	}
	else if (!strcmp(i_command, "off"))
	{
		setState(EState_Mute);
	}
	else if (!strcmp(i_command, "play"))
	{
		CSound* sound = CreateSound(i_parameters);
		if (sound)
		{
			sound->Play();
		}
	}
}

void CSoundMgr::SetIsSoundOn(bool i_is)
{
	if (i_is)
	{
		setState(EState_On);
	}
	else
	{
		setState(EState_Mute);
	}
}

void CSoundMgr::setState(EState i_state)
{
	if (i_state == m_state)
	{
		return;
	}
	
	m_state = i_state;
	
	switch (m_state)
	{
	case EState_On:
		{
			// turn on the volume!
		}
		break;
	case EState_Mute:
		{
			//turn off the volume of all sounds.
		}
		break;
	}
}

CSound* CSoundMgr::CreateSound(const char* i_filename)
{
	CSound* foundSound = GetLoadedSound(i_filename);
	if (foundSound)
	{
		return foundSound;
	}
	
	//if this sound isn't already loaded... then load one.
	unsigned int soundId = 0;
	if (s_systemSoundLoader(i_filename, soundId))
	{
		CSound* newSound = new CSound(soundId);
		m_sounds.push_back(newSound);
		m_soundFileNames.push_back(std::string(i_filename));
		return newSound;
	}
	
	return NULL;
}

CSound* CSoundMgr::GetLoadedSound(const char* i_fileName)
{
	//search through and find the texture
	CSound* foundSound = NULL;
	for (int iSound = 0; iSound < m_soundFileNames.size(); ++iSound)
	{
		if (m_soundFileNames[iSound] == i_fileName)
		{
			foundSound = m_sounds[iSound];
			break;
		}
	}

	return foundSound;
}
	
void CSoundMgr::Play(const CSound* i_sound)
{
	
}

void CSoundMgr::Stop(const CSound* i_sound)
{
	
}

/*-----------------------------------EOF!-----------------------------------*/ 